import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { FreightAccountService } from '../../services/freight-account.service';
import { ClearingModalComponent } from './clearing-modal/clearing-modal.component';

@Component({
  selector: 'app-abnormal-gold',
  templateUrl: './abnormal-gold.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class AbnormalGoldComponent extends BasicTableComponent implements OnInit {
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  rechargeStatus = '1';
  constructor(
    public service: FreightAccountService,
    private nzModalService: NzModalService,
    private router: Router,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
  }

  ngOnInit(): void {}

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    Object.assign(requestOptions.body, { rechargeStatus: this.rechargeStatus });
    if (this.sf) {
      Object.assign(requestOptions.body, { ...this.sf?.value });
    }
    return requestOptions;
  };

  changePaymentStatus(status: string) {
    this.rechargeStatus = status;
    this.st.load(1);
  }

  refund(item: any) {
    this.nzModalService.warning({
      nzTitle: '确定要将该笔款项原路退回？',
      nzOnOk: () => {}
    });
  }

  clearingAction(item: any) {
    const modal = this.nzModalService.create({
      nzTitle: '清分',
      nzContent: ClearingModalComponent,
      nzOnOk: com => {
        return false;
      }
    });
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        paySerialNumber: {
          type: 'string',
          title: '银行流水号',
          ui: {
            placeholder: '请输入'
          }
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          }
        },
        bankType: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: '' },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        transferBankAccount: {
          type: 'string',
          title: '付款账户',
          ui: {
            placeholder: '请输入'
          }
        },
        transferBankCardNumber: {
          type: 'string',
          title: '付款账号',
          ui: {
            placeholder: '请输入'
          }
        },
        transferBankOpenName: {
          type: 'string',
          title: '付款银行',
          ui: {
            placeholder: '请输入'
          }
        },
        transferDate: {
          title: '转账时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择',
            nzShowTime: true
          } as SFDateWidgetSchema
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '银行流水号', index: 'paySerialNumber', width: 180 },
      { title: '网络货运人', index: 'ltdName', width: 220 },
      { title: '银行类型', index: 'bankTypeLabel', width: 100 },
      { title: '资金总账号', index: 'virtualAccount', width: 180 },
      {
        title: '充值金额',
        index: 'rechargeAmount',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.rechargeAmount }) }
      },
      { title: '付款账户', index: 'transferBankAccount', width: 180 },
      { title: '付款账号', index: 'transferBankCardNumber', width: 180 },
      { title: '付款银行', index: 'transferBankOpenName', width: 220 },
      { title: '转账时间', index: 'createTime', type: 'date', width: 150 },
      { title: '转账备注', index: 'rechargeRemark', width: 180 },
      { title: '退款时间', index: 'refundTime', type: 'date', width: 150 },
      // { title: '操作人', index: 'rechargeName', width: 140 },
      // { title: '操作时间', index: 'callNo', type: 'date', width: 160 },
      { title: '状态', index: 'rechargeStatusLabel', width: 110 },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 120,
        buttons: [
          {
            text: '查看',
            acl: { ability: ['FINANCIAL-ABNORMAL-view'] },
            click: item => this.router.navigate(['/financial-management/abnormal-gold/detail/' + item.id])
          }
        ]
      }
    ];
  }
  // 导出
  exprot() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_get_exportAbnormalAmountPage);
  }
}
